<template>
  <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" size="large">
    <el-form-item prop="phoneNumber">
      <el-input v-model="loginData.phoneNumber" placeholder="工号：">
        <template #prefix>
          <el-icon class="el-input__icon">
            <user />
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input v-model="loginData.password" type="password" placeholder="输入密码" show-password autocomplete="new-password">
        <template #prefix>
          <el-icon class="el-input__icon">
            <lock />
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
  </el-form>
  <div class="login-btn">
    <el-button :icon="CircleClose" round size="large" @click="resetForm(loginFormRef)"> 重置 </el-button>
    <el-button :icon="UserFilled" round size="large" type="primary" :loading="loading" @click="login"> 登录 </el-button>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onBeforeUnmount } from "vue";
import { useRouter } from "vue-router";
import { HOME_URL } from "@/config";
// import { getTimeState } from "@/utils";
import { Login } from "@/api/interface";
import { userLoginService } from "@/api/backoffice/login";
//import { loginApi } from "@/api/modules/login";
//import { useUserStore } from "@/stores/modules/user";
import { useTabsStore } from "@/stores/modules/tabs";
import { useKeepAliveStore } from "@/stores/modules/keepAlive";
import { initDynamicRouter } from "@/routers/modules/dynamicRouter";
import { CircleClose, UserFilled } from "@element-plus/icons-vue";
import type { ElForm } from "element-plus";
//import md5 from "md5";
import { useTokenStore } from "@/stores/token";
import { usePhoneStore } from "@/stores/phone";

const router = useRouter();
//const userStore = useUserStore();
const tabsStore = useTabsStore();
const keepAliveStore = useKeepAliveStore();

type FormInstance = InstanceType<typeof ElForm>;
const loginFormRef = ref<FormInstance>();

const loginRules = reactive({
  //phoneNumber: [{ required: true, message: "请输入工号", trigger: "change" }],
  //password: [{ required: true, message: "请输入密码", trigger: "blur" }]
});

const loading = ref(false);
const loginForm = reactive<Login.ReqLoginForm>({
  username: "",
  password: ""
});

//开发区
const tokenStore = useTokenStore();
const phoneStore = usePhoneStore();

//用户登录数据模型
const loginData = ref({
  phoneNumber: "",
  password: ""
});

// login
const login = async () => {
  loading.value = true;
  try {
    // 1.执行登录接口
    console.log(loginData.value);
    const data = await userLoginService(loginData.value);
    //设置本地持久化
    tokenStore.setToken(data.data.desc);
    phoneStore.setPhone(loginData.value.phoneNumber);

    console.log(tokenStore.token);

    // 2.添加动态路由
    await initDynamicRouter();
    console.log("添加成功");
    // 3.清空 tabs、keepAlive 数据
    tabsStore.setTabs([]);
    keepAliveStore.setKeepAliveName([]);
    console.log("准备跳转");
    // 4.跳转到首页
    router.replace(HOME_URL);
  } finally {
    loading.value = false;
  }
};

// resetForm
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

onBeforeUnmount(() => {
  document.onkeydown = null;
});
</script>

<style scoped lang="scss">
@import "../index";
</style>
